﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using GestionBudget.Objects;

namespace GestionBudget.Controls
{
    public partial class ucProfil : ControlObject
    {
        public string CellValue;

        public ucProfil()
        {
            InitializeComponent();

            dgvProfils.AutoGenerateColumns = false;

            DataGridViewComboBoxColumn deviseColumn = (DataGridViewComboBoxColumn)dgvProfils.Columns["cDevise"];
            deviseColumn.DisplayMember = "Description";
            deviseColumn.ValueMember = "Self";
            deviseColumn.DataPropertyName = "Devise";
        }

        public override string TitreControl
        {
            get
            {
                return "Gestion des profils";
            }
        }

        public override bool Ajouter()
        {
            Profil pr = Profil.Creer();
            pr.AttribuerIdentifiant();

            ChargerListe();
            return true;
        }

        public override void Sauvegarder()
        {
            dgvProfils.EndEdit();
            
            foreach (ObjectPersistent profil in DictionnaireDonnees.ListeProfils.Where(pr => pr.EstNouveau || pr.EstModifie))
            {
                profil.Sauvegarder();
            }

            ChargerListe();
        }

        public override bool Supprimer()
        {
            if (dgvProfils.SelectedRows.Count > 0)
            {
                foreach(DataGridViewRow selectedRow in dgvProfils.SelectedRows)
                {
                    if (!((Profil)selectedRow.DataBoundItem).ToString().Equals(Securite.ProfilCourant.ToString()))
                    {
                        DictionnaireDonnees.Supprimer((ObjectPersistent)selectedRow.DataBoundItem);
                    }
                }

                ChargerListe();
                return true;
            }
            else
            {
                return false;
            }

        }

        public override void ChargerListe()
        {
            if (Securite.ProfilCourant.Nom != "Administrateur" && Securite.ProfilCourant.Prenom != "Administrateur")
            {
                dgvProfils.DataSource = DictionnaireDonnees.ListeProfils.Where(pr => pr.Identifiant == Securite.ProfilCourant.Identifiant).ToList();
            }
            else
            {
                dgvProfils.DataSource = DictionnaireDonnees.ListeProfils.ToList();
            }
            

            DataGridViewComboBoxColumn deviseColumn = (DataGridViewComboBoxColumn)dgvProfils.Columns["cDevise"];
            deviseColumn.DataSource = DictionnaireDonnees.ListeDevises.ToList();
        }

        private void dgvProfils_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {             
            DataGridView dgv = sender as DataGridView;
            Object nouvelleValeur;

            if (dgv.CurrentCell != null && CellValue != null)
            {
                nouvelleValeur = ((sender as DataGridView).CurrentCell is DataGridViewComboBoxCell ? ((sender as DataGridView).CurrentRow.DataBoundItem as Profil).Devise.Description : dgv.CurrentCell.Value);
  
                if (CellValue.Equals(nouvelleValeur) && (!String.IsNullOrEmpty(nouvelleValeur.ToString())))
                {
                    (this.Parent as FormPrincipal).GestionBouton();
                }

                else if (String.IsNullOrEmpty(nouvelleValeur.ToString()))
                {
                    dgv.CurrentCell.Value = CellValue;
                }
            }
        }

        private void dgvProfils_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
        {
            if ((sender as DataGridView).CurrentCell is DataGridViewComboBoxCell)
            {
                CellValue = ((sender as DataGridView).CurrentRow.DataBoundItem as Profil).Devise.Description;
            }
            else
            { 
                CellValue = (sender as DataGridView).CurrentCell.Value.ToString();
            } 
        }

        private void dgvDevises_DataError(object sender, DataGridViewDataErrorEventArgs e)
        {
            MessageBox.Show("La valeur n'est pas valide.", "Avertissement", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
        }
    }
}
